package com.dtjd.playcoinmonkey.activities;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;

import androidx.annotation.MainThread;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityOptionsCompat;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTSplashAd;
import com.dtjd.playcoinmonkey.activities.main.MainActivity;
import com.dtjd.playcoinmonkey.databinding.ActivitySplashAdBinding;
public class SplashAdActivity extends AppCompatActivity {
    private ActivitySplashAdBinding binding;
    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        binding = ActivitySplashAdBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        TTAdNative mTTAdNative = TTAdSdk.getAdManager().createAdNative(this);
        mTTAdNative.loadSplashAd(new AdSlot.Builder().setCodeId("887592833").setSupportDeepLink(true).setImageAcceptedSize(1080,1920).build(),new TTAdNative.SplashAdListener() {
            @Override
            @MainThread
            public void onError(int code, String message){
                goToMainActivity();
            }
            @Override
            @MainThread
            public void onTimeout() {
                goToMainActivity();
            }
            @Override
            @MainThread
            public void onSplashAdLoad(TTSplashAd ad) {
                if (ad == null) return;
                View view = ad.getSplashView();
                if ( !SplashAdActivity.this.isFinishing()) {
                    binding.splashAds.removeAllViews();
                    binding.splashAds.addView(view);
                }else{
                    goToMainActivity();
                }
                ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener(){
                    @Override public void onAdClicked(View view, int type){}
                    @Override public void onAdShow(View view, int type){}
                    @Override public void onAdSkip() { goToMainActivity();}
                    @Override public void onAdTimeOver(){ goToMainActivity();}
                });
                ad.getInteractionType();
            }
        }, 3000);
    }
    private void goToMainActivity(){
        if(checkDoubleClick()){
            startActivity(new Intent(SplashAdActivity.this, MainActivity.class), ActivityOptionsCompat.makeSceneTransitionAnimation(SplashAdActivity.this, binding.getRoot(), "transition").toBundle());
            finish();
        }
    }
    private long mClickTime;
    private boolean checkDoubleClick(){
        boolean result= mClickTime < SystemClock.uptimeMillis() - 3000;
        mClickTime = SystemClock.uptimeMillis();
        return result;
    }
}